<?php

namespace App\Module\Admin\Controllers;

use App\System\Cms\Repositories\CmsTopicAlbumRepository;
use App\System\Cms\Repositories\CmsTopicRepository;
use App\System\Order\Repositories\OrderExpressListRepository;
use App\System\Order\Repositories\OrderInfoRepository;
use Illuminate\Http\Request;
use Excel;

/**
 * Class OrderController 订单管理
 * @package App\Module\Admin\Controllers
 */
class OrderController extends Controller
{
    /**
     * 订单管理
     */

    //订单列表
    public function index(Request $request)
    {
        $keyword = $request['keyword'];
        $startTime = $request['start_time'];
        $endTime = $request['end_time'];
        $status = $request['status'];
        $page = $request['page'];
        $where = ['keyword'=>$keyword,'start_time'=>$startTime,'end_time'=>$endTime,'status'=>$status,'page'=>$page];
        $order = OrderInfoRepository::getInstance();
        $data = $order->getList($keyword,$startTime,$endTime,$status);
        $statusArr = [1=>'待支付',2=>'待发货',3=>'已发货',4=>'已完成',5=>'已取消'];
        return view('admin.order.index',compact('data','where','statusArr'));
    }

    //订单详情
    public function view(Request $request)
    {
        $id = $request['id'];
        $order = OrderInfoRepository::getInstance();
        $data = $order->getDetail($id);
        $statusArr = [1=>'待支付',2=>'待发货',3=>'已发货',4=>'已完成',5=>'已取消'];
        $express = OrderExpressListRepository::getInstance();
        $list = $express->getList(1);
        return view('admin.order.view',compact('data','statusArr','list'));
    }

    //下载订单列表
    public function downLoad(Request $request)
    {
        $keyword = $request['keyword'];
        $startTime = $request['start_time'];
        $endTime = $request['end_time'];
        $status = $request['status'];
        $order = OrderInfoRepository::getInstance();
        $data = $order->getList($keyword,$startTime,$endTime,$status,true);
        $statusArr = [1=>'待支付',2=>'待发货',3=>'已发货',4=>'已完成',5=>'已取消'];
        $headArr = array('订单编号','收货人','总金额','应付金额','支付状态','订单状态','下单时间');
        foreach ($data as $v) {
            $thearr = array();
            $thearr['order_sn'] = $v->order_sn;
            $thearr['mobile'] = $v->consignee.':'.$v->mobile;
            $thearr['order_amount'] = $v->order_amount;
            $thearr['pay_amount'] = $v->pay_amount;
            $thearr['pay_time'] = $v->pay_time?'已支付':'未支付';
            $thearr['status'] = $statusArr[$v->status];
            $thearr['created_at'] = $v->created_at;
            $arr[] = $thearr;
        }
        $this->getExcel('订单列表',$headArr,$arr,['order_amount','pay_amount']);
    }

    // 修改订单
    public function ajaxSave(Request $request)
    {
        if ($request->isMethod('post')) {
            $thepost = $request->only('id','shipping_name', 'shipping_code', 'remark','status');
            $updata['shipping_name'] = $thepost['shipping_name']??'';
            $updata['shipping_code'] = $thepost['shipping_code']??'';
            $updata['admin_message'] = $thepost['remark']??'';
            $updata['status'] = $thepost['status']??'';
            if($updata['status']==3){
                $updata['shipping_time'] = date('Y-m-d H:i:s',time());
            }
            $id = $thepost['id'];
            $order = OrderInfoRepository::getInstance();
            $res = $order->updateData($id, $updata);
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            $url = !empty($res['data']) ? url('/admin/order') : '';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => $url]);
        }
    }

}
